package org.detailsmatter.util.assertion;

import org.apache.commons.lang.StringUtils;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.hamcrest.core.SubstringMatcher;

public class StringContainsOccurrence extends SubstringMatcher {
	private final int nb;

	public StringContainsOccurrence(int nb, String pattern) {
		super(pattern);
		this.nb = nb;
	}

	@Override
	protected boolean evalSubstringOf(String s) {
		return StringUtils.countMatches(s, substring) == nb;
	}

	@Override
	protected String relationship() {
		return "containing exactly " + nb;
	}

	@Factory
	public static Matcher<String> containsExactly(int nb, String pattern) {
		return new StringContainsOccurrence(nb, pattern);
	}
}
